package com.huazi.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huazi.commonutils.CommentResult;
import com.huazi.eduorder.entity.TPayLog;
import com.huazi.eduorder.service.TPayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author huazi
 * @since 2021-03-30
 */
@RestController
@CrossOrigin
@RequestMapping("/eduorder/pay-log")
public class TPayLogController {//微信支付接口
    @Autowired
    TPayLogService payLogService;

    //生成微信支付的二维码
    @GetMapping("createNative/{memberId}/{orderNo}")
    public CommentResult createNative(@PathVariable String memberId,@PathVariable String orderNo){
        Map map=payLogService.createNative(memberId, orderNo);
        System.out.println("返回二维码的map集合："+map);
        return CommentResult.getOK().data("map",map);
    }
    //根据会员id查询支付的状态
    @GetMapping("queryPayStatus/{memberId}/{orderNo}")
    public CommentResult queryPayStatus(@PathVariable String memberId,@PathVariable String orderNo){
        Map<String,String> map=payLogService.queryPayStatus(memberId,  orderNo);
        System.out.println("查询订单状态的map集合："+map);
        if(map==null){
            return CommentResult.getError().data("msg","支付出错了");
        }
        //如果返回里面不为空，通过map获取订单状态
//        System.out.println(map.get("trade_state"));
        if(map.get("trade_state").equals("SUCCESS")){
            //支付成功后添加订单日志.更新订单表的订单状态
            payLogService.updateOrderStatus(map);
            return CommentResult.getOK()
                    .data("map",map).data("success",true);//记录本次所有订单id
        }
        return CommentResult.getOK().data("success",false);
    }
    //根据会员id和课程id判断是否已经购买课程
    @GetMapping("getOrderBuyYes/{memberId}/{courseId}")
    public CommentResult getOrderBuyYes(@PathVariable String memberId,@PathVariable String courseId){
        TPayLog one = payLogService.getOne(new QueryWrapper<TPayLog>().eq("member_id", memberId).eq("course_id", courseId));
        if(one!=null) return CommentResult.getOK();
        return CommentResult.getError();
    }
}

